<?
header('Content-Type: text/html; charset=UTF-8');
require 'crawl.php';
require '../libs/function.php';
require 'database.php';
$db = new DB();

$parrent = isset($_GET['parrent']) ? strval($_GET['parrent']) : NULL;
$cat = isset($_GET['c']) ? strval($_GET['c']) : NULL;
$cat = str_replace(' ', '+', $cat);

if ($parrent)
	$p = isset($_GET['p']) ? strval($_GET['p']) : 1;
else
	$p = isset($_GET['p']) ? strval($_GET['p']) : 1;

$page = '';
if ($p)
	$page = '/' . $p;

$url = 'http://www.gamepilot.com/games/' . $cat . $page;

$total_games = crawl($url, $parrent, $cat);

$next = '';
if ($parrent) {
	if ($total_games >= 40) {
		if ($p > 0) {
			$next = '/_crawl/get-link-games.php?parrent=' . $parrent . '&c=' . $cat . '&p=' . ($p + 1);
			header('Location:' . $next, true, 301);
			exit();
		}
	}
} else {
	if ($p > 0) {
		$next = '/_crawl/get-link-games.php?c=' . $cat . '&p=' . ($p + 1);
		header('Location:' . $next, true, 301);
		exit();
	}
}

function crawl($url, $parrent, $cat) {
	global $db, $p;
	$array = array();

	$html = file_get_html($url);

	if ($html->find('div#games-container-category', 0))
		$string = $html->find('div#games-container-category', 0)->innertext;
	else
		$string = '';

	if (!$string) {
		return false;
		$html->clear();
		unset($html);
	}

	$str = str_get_html($string);

	foreach ($str->find('a') as $element){
		$plaintext = trim($element->plaintext);
		$plaintext = str_replace("'", '', $plaintext);
		$link = trim($element->href);

		$array['name'][] = $plaintext;
		$array['url'][] = $link;
	}

	foreach ($str->find('img') as $element){
		$img = trim($element->src);
		$array['image'][] = $img;
	}

// 	echo '<pre>';
// 	print_r($array);
// 	echo '</pre>';
// 	exit();

	$total = count($array['url']);

	$num = $duplicate = 0;
	for ($i = 0; $i < $total; $i++) {
		// check url exists
		$Qry = 'SELECT crawl_url
				FROM games
				WHERE crawl_url = "' . $array['url'][$i] . '"';

		if ($db->numRows($Qry) == 0) {
			++$num;

			$game_url = remove_unicode($array['name'][$i], true);
// 			$cat = remove_unicode($cat, true, true);
			$cat = strtolower($cat);

			if ($parrent) {
				$data = array(
						'g_name'=>$array['name'][$i],
						'g_url'=>$game_url,
						'g_image'=>$array['image'][$i],
						'category'=>$parrent,
						'tags'=>$cat,
						'updated'=>(time() + rand(10, 99)),
						'active'=>0,
						'crawl_url'=>$array['url'][$i]
				);
			} else {
				$data = array(
						'g_name'=>$array['name'][$i],
						'g_url'=>$game_url,
						'g_image'=>$array['image'][$i],
						'category'=>$cat,
						'updated'=>(time() + rand(10, 99)),
						'active'=>0,
						'crawl_url'=>$array['url'][$i]
				);
			}

// 			echo '<pre>';
// 			print_r($data);
// 			echo '</pre>';

			$newID = $db->insert('games', $data, true);
		} else {
			// Update category
			$Qry = 'SELECT g_id, category, tags
					FROM games
					WHERE crawl_url = "' . $array['url'][$i] . '"';
			if ($db->numRows($Qry) > 0) {
				$row = $db->fetchAll($Qry);

				$c_array = explode(',', $row[0]['category']);

				if (in_array($cat, $c_array) !== true) {
					++$duplicate;
// 					$sth = array('_', '+', ' ');

					if ($parrent) {
						if ($row[0]['category'])
							$new_category = $row[0]['category'] . ',' . $parrent;
						else
							$new_category = $parrent;
						$new_category = explode(',', $new_category);
						$new_category = array_unique($new_category);
						$new_category = implode(',', $new_category);
// 						$new_category = str_replace($sth, '-', $new_category);

						if ($row[0]['tags'])
							$new_tags = $row[0]['tags'] . ',' . $cat;
						else
							$new_tags = $cat;
						$new_tags = explode(',', $new_tags);
						$new_tags = array_unique($new_tags);
						$new_tags = implode(',', $new_tags);
// 						$new_tags = str_replace($sth, '-', $new_tags);

						$query = 'UPDATE games SET category = "' . $new_category . '", tags = "' . $new_tags . '" WHERE g_id = ' . $row[0]['g_id'];
					} else {
						$new_category = $row[0]['category'] . ',' . $cat;
						$query = 'UPDATE games SET category = "' . $new_category . '" WHERE g_id = ' . $row[0]['g_id'];
					}

					$db->Query($query);
				}
			}
		}
	}

	echo 'Total games added: ' . $num . ' on page ' . $p . '<br />';
	echo 'Duplicate URL: ' . $duplicate;

	return $num + $duplicate;
}